using ProtoModels;
using UnityEngine;

public class GGMessageButton : MonoBehaviour
{
	public UISprite defaultImage;

	public UITexture imageTexture;

	public UILabel title;

	public UILabel sender;

	public GameObject loadingDisplay;

	private ServerMessages.GGServerMessage message;

	private MessageDisplayController display;

	public bool isGroupAccepting { get; private set; }

	public void Init(ServerMessages.GGServerMessage message, int groupId, MessageDisplayController display)
	{
		isGroupAccepting = false;
		this.message = message;
		this.display = display;
		UpdateUI();
		GGServerRequestsBackend.TextureRequest textureRequest = new GGServerRequestsBackend.TextureRequest(message.imageUrl);
		textureRequest.groupId = groupId;
		textureRequest.onComplete = OnTextureFetchComplete;
		textureRequest.cache = CacheStategy.GetFromCache;
		BehaviourSingleton<GGRequestLimitter>.instance.Add(textureRequest);
	}

	private void UpdateUI()
	{
		defaultImage.gameObject.SetActive(message.imageUrl == string.Empty);
		UITools.ChangeText(title, message.title);
		UITools.ChangeText(sender, "- " + message.sender.name);
		loadingDisplay.SetActive(false);
	}

	public void OnClickMessage()
	{
		TableTennisDialog.instance.ShowYesNo("Message", message.message + "\n\n - " + message.sender.name, "Accept", "Dismiss", onDialogComplete);
	}

	public void onDialogComplete(bool success)
	{
		loadingDisplay.SetActive(true);
		if (success)
		{
			BehaviourSingletonInit<GGServerMessages>.instance.MarkMessageAsRead(message.requestId, ApplyMessageOnSuccess);
		}
		else
		{
			BehaviourSingletonInit<GGServerMessages>.instance.MarkMessageAsRead(message.requestId, RejectMessageOnSuccess);
		}
		NavigationManager.instance.Pop();
	}

	public void ApplyMessageOnSuccess(GGServerRequestsBackend.ServerRequest request)
	{
		if (request.status != 0)
		{
			DisplayErrorWithServer();
			return;
		}
		foreach (ServerMessages.GGServerMessage.Attachment attachment in message.attachments)
		{
			BehaviourSingletonInit<GGServerMessages>.instance.ExecuteMessageAttachment(attachment.functionKey, attachment);
		}
		DisableButtonAfterProcessing();
	}

	public void RejectMessageOnSuccess(GGServerRequestsBackend.ServerRequest request)
	{
		if (request.status != 0)
		{
			DisplayErrorWithServer();
		}
		else
		{
			DisableButtonAfterProcessing();
		}
	}

	private void DisplayErrorWithServer()
	{
		TableTennisDialog.instance.ShowOk("Server currently unavailable", "Server currently unavailable!\n Please try again later", "Ok", delegate
		{
			loadingDisplay.SetActive(false);
			NavigationManager.instance.Pop();
		});
	}

	private void DisableButtonAfterProcessing()
	{
		base.gameObject.SetActive(false);
		display.SortTable();
	}

	public string GetIdAndBeginAccept()
	{
		loadingDisplay.SetActive(true);
		isGroupAccepting = true;
		return message.requestId;
	}

	public bool CanAccept()
	{
		return base.gameObject.activeSelf && !loadingDisplay.activeSelf;
	}

	public void OnTextureFetchComplete(GGServerRequestsBackend.ServerRequest request)
	{
		if (request.status == GGServerRequestsBackend.ServerRequest.RequestStatus.Success)
		{
			imageTexture.mainTexture = request.GetResponse<Texture2D>();
			display.OnTextureArrived();
		}
	}
}
